package com.ruicar.afs.cloud.batch.dayend.process;

import com.ruicar.afs.cloud.batch.dayend.handle.run.SystemJdbcConfig;
import com.ruicar.afs.cloud.batch.frame.AbsBatchRunner;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.util.SpringContextHolder;
import com.ruicar.afs.cloud.common.util.EmptyUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * <p>Description: 每日待扣款数据归档</p>
 *
 * @author ZC.GUO
 * @version 1.0
 * @date create on 2020-08-26 21:15
 */
public class DebitRecordArchiveBatchRunner extends AbsBatchRunner {
    private SystemJdbcConfig systemJdbcConfig= SpringContextHolder.getBean(SystemJdbcConfig.class);
    @Override
    public void process(String param) {
        Connection contract = this.getConnection(systemJdbcConfig.getBasic());
        try{
            /**  没有正在扣款中的待扣款数据 by ZC.GUO  **/
            if (this.recordArchiveCondition(contract)){
                /**  数据归档 by ZC.GUO  **/
                this.recordArchive(contract);
            }else{
                throw new AfsBaseException("有正在扣款中的待扣款数据,待扣款数据归档失败");
            }
        }catch (Exception e){
            logger.error("",e);
            throw new AfsBaseException(e.getMessage());
        }finally {
            clean(contract);
        }
    }

    public void recordArchive(Connection contract){
        StringBuffer insertSql = new StringBuffer();
        insertSql.append("insert into basic_debit_record_history(");
        insertSql.append("  id,contract_no,batch_id,fee_type,source_table,source_id,ready_amt,succeed_amt,going_amt,debit_status,debit_sign_bank,");
        insertSql.append("  debit_sign_code,debit_ebank_code,debit_account,debit_account_name,debit_card_phone,debit_cert_no,credit_bank_code,");
        insertSql.append("  credit_account,term_no,write_off_amt,plan_date,record_date,manual_num,revision,create_by,create_time,del_flag,");
        insertSql.append("  debit_bank_branch,overdue_days,store_date) ");
        insertSql.append("select ");
        insertSql.append(" rec.id,rec.contract_no,rec.batch_id,rec.fee_type,rec.source_table,rec.source_id,rec.ready_amt,rec.succeed_amt,");
        insertSql.append(" rec.going_amt,rec.debit_status,rec.debit_sign_bank,rec.debit_sign_code,rec.debit_ebank_code,rec.debit_account,");
        insertSql.append(" rec.debit_account_name,rec.debit_card_phone,rec.debit_cert_no,rec.credit_bank_code,rec.credit_account,rec.term_no,");
        insertSql.append(" rec.write_off_amt,rec.plan_date,rec.record_date,rec.manual_num,rec.revision,'system',NOW(),0,rec.debit_bank_branch,");
        insertSql.append(" rec.overdue_days,NOW() ");
        insertSql.append("from basic_debit_record rec");
        PreparedStatement insertPs = null;

        StringBuffer deleteSql = new StringBuffer();
        deleteSql.append("delete from basic_debit_record");
        PreparedStatement deletePs = null;
        try {
            insertPs = contract.prepareStatement(insertSql.toString());
            insertPs.execute();
            deletePs = contract.prepareStatement(deleteSql.toString());
            deletePs.execute();
            contract.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AfsBaseException(e.getMessage());
        }finally {
            clean(insertPs);
            clean(deletePs);
        }
    }

    public boolean recordArchiveCondition(Connection contract){
        boolean flag = true;
        StringBuffer querySql = new StringBuffer();
        querySql.append("select count(id) num from basic_debit_record rec where rec.going_amt > 0 or rec.debit_status = 'debiting'");
        PreparedStatement queryPs = null;
        ResultSet queryRs = null;
        try {
            queryPs = contract.prepareStatement(querySql.toString());
            queryRs = queryPs.executeQuery();
            if(EmptyUtils.isNotEmpty(queryRs) && queryRs.next()){
                /**  正在扣款中指令数量 by ZC.GUO  **/
                Integer num = queryRs.getInt("num");
                if (EmptyUtils.isNotEmpty(num) && num > 0){
                    flag = false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new AfsBaseException(e.getMessage());
        }finally{
            clean(queryPs);
            clean(queryRs);
        }
        return flag;
    }
}
